package com.market.product.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.market.options.dao.OptionsDao;
import com.market.options.domain.OptionsCommand;
import com.market.product.dao.ProductDao;
import com.market.product.domain.ProductCommand;

@Controller
public class ProDetailController {
	@Autowired
	private ProductDao productDao;
	@Autowired
	private OptionsDao optionsDao;
	
	@ModelAttribute("command")
	public OptionsCommand initCommand(){
		return new OptionsCommand();
	}	

	@RequestMapping("/productDetail.do")
	public ModelAndView form(@RequestParam("p_code") int p_code,			
			@RequestParam("pageNum") int pageNum,
			@RequestParam("searchMarket") String searchMarket,
			@RequestParam("searchCategory") String searchCategory){
		
		ProductCommand product = productDao.select(p_code);
		List<OptionsCommand> options = optionsDao.list(p_code);		
		String images[] = product.getImage().split("&");	
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("productDetail");
		mav.addObject("product", product);
		mav.addObject("options", options);
		mav.addObject("pageNum", pageNum);		
		mav.addObject("searchMarket", searchMarket);
		mav.addObject("searchCategory", searchCategory);
		mav.addObject("image",images[0]);		
		return mav;
	}
}
